package Server.ServerCore.Service.syns;

import Server.ExtComponents.BaseConfig.MServerConfig;
import Server.ServerCore.Model.AI.BankerAI;
import Server.ServerCore.Model.Player.GameCharacter;
import javolution.util.FastList;
import org.springframework.stereotype.Service;

/**
 * @author :石头哥哥<br/>
 *         Project:21Server
 *         Date: 13-4-2
 *         Time: 上午11:06
 *         房间管理
 */
@Service
public class TableManager{

    public static FastList<Table> tableFastList;//从右往左，0，1，2，3，4

    public void initTables(){
        tableFastList=new FastList<Table>(MServerConfig.TableNum);
        for (int ID=0;ID!=MServerConfig.TableNum;++ID){
            Table table=new Table(ID);
            tableFastList.add(table);
            BankerAI banker=new BankerAI();
            banker.setID(ID);//编号
            banker.setCircleUnitID(5);//this Fixed is banker seat!
            banker.setTableID(ID);
            table.setBanker(banker);
            banker.IntoTable();
        }
    }

    /**
     * 匹配牌桌和当前牌桌的位置
     * @return int[0]:tableID,int[1]:CircleUnitID
     */
    public static int[] getFreeTable(){
        int[]infor=new int[2];
        for (Table table:tableFastList){
            FastList<GameCharacter> gameCharacterFastList=table.getGameCharacterFastList();
            if (gameCharacterFastList.size()==6)continue;//当前房间已满
            FastList<Table.CircleUnit> circleUnitFastList=table.getCircleUnitFastList();
            for (Table.CircleUnit circleUnit:circleUnitFastList){
                if (circleUnit.isOccupancy()||circleUnit.getCircleUnitID()==5)continue;//被占用,查找没有被占用的CircleUnit,5为庄家
                infor[0]=table.getTableID();
                infor[1]=circleUnit.getCircleUnitID();
                circleUnit.setOccupancy(true);
                return infor;
            }

        }
        return infor;
    }

}
